package com.cqc.screencapture01;

import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.SurfaceView;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

/**
 * 如果MediaProjection不为null，那么设置VirtualDisplay
 * <p>
 * 如果resultCode不为0 + resultData不为null，那么重写设置MediaProjection+VirtualDisplay
 * <p>
 * 否则
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ToggleButton togBtn;
    private MediaProjectionManager mediaManager;
    private SurfaceView surfaceView;
    private Surface surface;
    private int densityDpi;
    int mResultCode;
    Intent mResultData;
    private MediaProjection mediaProjection;
    private VirtualDisplay mVirtualDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        togBtn = (ToggleButton) findViewById(R.id.togBtn);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surface = surfaceView.getHolder().getSurface();

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        densityDpi = metrics.densityDpi;

        mediaManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        togBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LogUtil.d(TAG, String.valueOf(isChecked));
                if (isChecked) {
                    if (surface == null) {
                        LogUtil.d(TAG, "surface == null");
                        return;
                    }
                    if (mediaProjection != null) {
                        createVirtualDisplay();
                    } else if (mResultCode != 0 && mResultData != null) {
                        getMediaProjection();
                        createVirtualDisplay();
                    } else {
                        startActivityForResult(mediaManager.createScreenCaptureIntent(), 1);//createScreenCaptureIntent() API>=21
                    }
                } else {
                    stopScreenCapture();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode != RESULT_OK) {
                LogUtil.d(TAG, "用户点击了取消");
                return;
            }
            mResultCode = resultCode;
            mResultData = data;

            getMediaProjection();
            createVirtualDisplay();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopScreenCapture();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaProjection != null) {
            mediaProjection.stop();
            mediaProjection = null;
        }
    }

    private void stopScreenCapture() {
        if (mVirtualDisplay != null) {
            mVirtualDisplay.release();//API>=19
            mVirtualDisplay = null;
        }
    }


    private void getMediaProjection() {
        mediaProjection = mediaManager.getMediaProjection(mResultCode, mResultData);
    }

    private void createVirtualDisplay() {
        mVirtualDisplay = mediaProjection.createVirtualDisplay("VirtualDisplay01", surfaceView.getWidth(),
                surfaceView.getHeight(), densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, surface, null, null);
    }
}
